# coding:utf-8

result = []

km_list = []

with open("report.txt") as f:
    yuan = f.readlines()


for cycle in yuan:
    kmshu = cycle.split()
    n_kmshu = kmshu[1:]
    s_kmshu = len(n_kmshu)               #列出总共有几科

for km in range(s_kmshu):
    km_zf = 0
    for y in yuan:
        km_e = y.split()
        n_km_e = km_e[1:]
        km_zf = km_zf + int(n_km_e[km])
    km_zf_avg = float(km_zf) / len(yuan)
    km_zf_AVG = "%.1f" % km_zf_avg
    km_list.append(km_zf_AVG)            #算出每科的平均分

km_list.insert(0,"平均")

#km_result.append(km_list)

#print km_list


for i in yuan:
    everyone = i.split()
    new_everyone = everyone[1:]
    zongfen = 0
    num = 0
    for j in new_everyone:
        zongfen += int(j)
        num += 1
    avg = float(zongfen) / num
    Avg = "%.1f" % avg
    everyone.append(str(zongfen))
    everyone.append(str(Avg))              #算出没人的总分和平均分
    for m in everyone[1:-2]:
        if int(m) < 60:
            everyone[everyone.index(m)] = "不及格"
    result.append(everyone)

result.sort(key=lambda r: r[-1], reverse=True)

result.insert(0,km_list)

#print result


title = ["名次", "姓名", "语文", "数学", "英语", "物理", "化学", "生物", "政治", "历史", "地理", "总分", "平均分"]
new_title = " ".join(title)
# print new_title

with open("result.txt", "a") as final:
    final.write(new_title)
    final.write("\n")

paiming = 0

for k in result:
    k.insert(0, str(paiming))
    paiming += 1
    x = " ".join(k)
    with open("result.txt", "a") as final:
        final.write(x)
        final.write("\n")
